package com.knowif.search;

/**
 * @author ：Xh
 * @date ：Created in 2019/5/3 0003 14:11
 */

import com.knowif.util.commonUtil.DateUtils;
import com.knowif.util.commonUtil.StringUtil;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchCondition {
    private static final String TYPE_CONSTANT_DATE = "Timestamp";
    protected int pageNumber = 0;
    protected int pageSize = 0;

    /**
     * 查询条件集合
     */
    List<Condition> conditionList;

    /**
     * 排序字段集合
     */
    Sort sort;

    public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public List<Condition> getConditionList() {
		return conditionList;
	}

	public void setConditionList(List<Condition> conditionList) {
		this.conditionList = conditionList;
	}

	public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    public String getInString(String inKey, String field) {
        StringBuilder subQ = new StringBuilder();
        subQ.append("(");
        if (!"".equals(inKey) && inKey != null) {
            String[] inKeys = inKey.split(",");
            if (inKeys.length > 1000) {
                for (int i = 0; i < inKeys.length; ++i) {
                    if ((i + 1) % 1000 == 0) {
                        String temp = subQ.substring(0, subQ.length() - 1);
                        subQ.delete(0, subQ.length());
                        subQ.append(temp);
                        subQ.append(") or ");
                        subQ.append(field);
                        subQ.append(" in (");
                        subQ.append(inKeys[i] + ",");
                    } else {
                        subQ.append(inKeys[i] + ",");
                    }
                }

                String temp = subQ.substring(0, subQ.length() - 1);
                subQ.delete(0, subQ.length());
                subQ.append(temp);
                subQ.append(")");
            } else {
                subQ.append(inKey);
                subQ.append(")");
            }
        } else {
            subQ.append(")");
        }

        return subQ.toString();
    }


    /**
     * 获取查询参数
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> getParameterMap() {
        Map<String, Object> map = new HashMap<>();
        if (this.getConditionList() != null && !this.getConditionList().isEmpty())
            for (Condition condition : conditionList) {
//                String key = getSearchKey(condition);
                Object value = condition.getValue();
                if (value instanceof String && StringUtils.isBlank(value.toString())) {
                    continue;
                }
                if (value instanceof Map) {
                    Map<String, Object> valueMap = new HashMap<>();
                    valueMap = (Map<String, Object>) value;
                    value = valueMap.get("value");
                }
                if (TYPE_CONSTANT_DATE.equals(condition.getType())) {
                    value = DateUtils.parseDate(value);
                }
                map.put(condition.getCode(), value);
            }
        return map;
    }

    /**
     * 获取查询参数(不带操作)
     *
     * @return
     */
    public Map<String, Object> getParameterValueMap() {
        Map<String, Object> map = new HashMap<>();
        if (this.getConditionList() != null && !this.getConditionList().isEmpty()) {
            for (Condition condition : conditionList) {
                if (TYPE_CONSTANT_DATE.equals(condition.getType())) {
                    map.put(condition.getCode(), DateUtils.parseDate(condition.getValue()));
                } else {
                    map.put(condition.getCode(), condition.getValue());
                }
            }
        }
        return map;
    }

    /**
     * @param contitionName
     * @return
     * @author: xingyao
     * @Description: 通过条件名称或者条件对应的值
     */
    public Object getContitionValue(String contitionName) {
        if (StringUtil.isEmpty(contitionName)) {
            return null;
        }
        Map<String, Object> map = getParameterValueMap();
        if (map.isEmpty()) {
            return null;
        }
        return map.get(contitionName);
    }

//    /**
//     * 获取查询字段名
//     *
//     * @param condition
//     * @return
//     */
//    private static String getSearchKey(Condition condition) {
//        String key = "";
//        switch (condition.getOperator()) {
//            case 1:
//                key = "EQ_";
//                break;
//            case 2:
//                key = "GT_";
//                break;
//            case 3:
//                key = "GTE_";
//                break;
//            case 4:
//                key = "LT_";
//                break;
//            case 5:
//                key = "LTE_";
//                break;
//            case 6:
//                key = "NEQ_";
//                break;
//            case 7:
//                key = "RLIKE_";
//                break;
//            case 8:
//                key = "LLIKE_";
//                break;
//            case 9:
//                key = "LIKE_";
//                break;
//            case 10:
//                key = "IN_";
//                break;
//            case 11:
//                key = "GGTE_";
//                break;
//            case 12:
//                key = "LLTE_";
//                break;
//        }
//
//        return key;
//    }

	@Override
	public String toString() {
		return "SearchCondition [pageNumber=" + pageNumber + ", pageSize=" + pageSize + ", conditionList="
				+ conditionList + ", sort=" + sort + "]";
	}
}
